package com.lb.jwitter.action;


import java.text.DateFormat; 
import java.text.SimpleDateFormat; 
import java.util.ArrayList; 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import java.util.Map; 
import org.springframework.test.context.ContextConfiguration; 
import com.lb.jwitter.service.TblQuyenChiTietService; 
import com.lb.jwitter.service.TblTaiKhoan; 
import com.opensymphony.xwork2.ActionContext; 
import java.util.Collection; 
import com.lb.jwitter.util.ApplicationContextProvider; 
import java.util.Date; 
import java.util.GregorianCalendar; 
import java.util.List;

import javax.xml.datatype.DatatypeFactory; 
import javax.xml.datatype.XMLGregorianCalendar; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 
import com.lb.jwitter.service.ConditionReport; 
import com.lb.jwitter.util.Message; 
import com.opensymphony.xwork2.Action; 
import com.lb.jwitter.service.TblDKMHDotDKHocVienChiTiet;
import com.lb.jwitter.service.TblDKMHDotDKHocVienChiTietService;
import com.lb.jwitter.service.TblDKMHDotDKNhom;
import com.lb.jwitter.service.TblDKMHDotDKNhomService;
import com.lb.jwitter.service.TblDKMHDotDK;
import com.lb.jwitter.service.TblDKMHDotDKService;

 /** 
 * @author QUOCVIET  
 * 
 */ 


@ContextConfiguration(locations="classpath:beans.xml") 
public class TblDKMHDotDKNhomUpdateAction{
    private int pageSize; 
    public int getPageSize() {
       return pageSize;
 }

    public void setPageSize(int pageSize) { this.pageSize = pageSize; }

    private int page; 
    public int getPage() { return page; }

    public void setPage(int page) { this.page = page; }

    private int pages; 
    public int getPages() { return pages; }

    public void setPages(int pages) { this.pages = pages; }

    private String dataString; 
    public String getDataString() { return dataString; }

    private Message msg; 
    public Message getMsg() { return msg; }

    private int id; 
    public int getId() {
       return id;
 }

    public void setId(int id) {
       this.id = id;
 }

    private String ma; 
    public void setMa(String ma) {
       this.ma = ma;
 }

    public String getMa() {
       return ma;
 }

    private String ten; 
    public void setTen(String ten) {
       this.ten = ten;
 }

    public String getTen() {
       return ten;
 }

    private int dotDKId; 
    public void setDotDKId(int dotDKId) {
       this.dotDKId = dotDKId;
 }

    public int getDotDKId() {
       return dotDKId;
 }

    private String ngayBatDau; 
    public void setNgayBatDau(String ngayBatDau) {
       this.ngayBatDau = ngayBatDau;
 }

    public String getNgayBatDau() {
       return ngayBatDau;
 }

    private String ngayKetThuc; 
    public void setNgayKetThuc(String ngayKetThuc) {
       this.ngayKetThuc = ngayKetThuc;
 }

    public String getNgayKetThuc() {
       return ngayKetThuc;
 }

    private String quyen; 
    public void setQuyen(String quyen) {
       this.quyen = quyen;
 }

    public String getQuyen() {
       return quyen;
 }

    private TblDKMHDotDKNhom tblDKMHDotDKNhom; 
    public void setTblDKMHDotDKNhom (TblDKMHDotDKNhom tblDKMHDotDKNhom) {
        this.tblDKMHDotDKNhom = tblDKMHDotDKNhom;
 }

    private Collection<TblDKMHDotDKNhom> tblDKMHDotDKNhoms; 
    public void setTblDKMHDotDKNhoms(Collection<TblDKMHDotDKNhom> tblDKMHDotDKNhoms) {
        this.tblDKMHDotDKNhoms = tblDKMHDotDKNhoms; 
      } 

    private TblDKMHDotDKNhomService tblDKMHDotDKNhomService; 

    private TblDKMHDotDKService tblDKMHDotDKService; 

	private int preUpdate(Message msg, TblDKMHDotDKNhom obj ){ 
        msg.setPreMessage_b(true); 
        msg.setPreMessage_s("success"); 
        return 1; 
    } 
	private int postUpdate(Message msg, TblDKMHDotDKNhom obj ){ 
        msg.setPostMessage_b(true); 
        msg.setPostMessage_s("success"); 
        return 1; 
    } 
  private TblTaiKhoan taiKhoanDangNhap; 
 public TblTaiKhoan getTaiKhoanDangNhap() { 
     return taiKhoanDangNhap; 
 } 
 public void setTaiKhoanDangNhap(TblTaiKhoan taiKhoanDangNhap) { 
     this.taiKhoanDangNhap = taiKhoanDangNhap; 
 } 
 private TblQuyenChiTietService tblQuyenChiTietService;
	 @Autowired // Injected by Spring 
     ApplicationContextProvider contextProvider; 
    public String execute() throws Exception 
   { 
    try{ 
     Map session = ActionContext.getContext().getSession();
     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
     if( taiKhoanDangNhap == null) return "noprivilege";
    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_DKMH_DotDK_Nhom", "sua")){ 
        return "noprivilege"; 
    }
        tblDKMHDotDKNhomService = (TblDKMHDotDKNhomService) context.getBean("tbl_DKMH_DotDK_NhomClient"); 
        tblDKMHDotDKService = (TblDKMHDotDKService) context.getBean("tbl_DKMH_DotDKClient");
        tblDKMHDotDKHocVienChiTietService = (TblDKMHDotDKHocVienChiTietService) context.getBean("tbl_DKMH_DotDK_HocVien_ChiTietClient"); 


        msg = new Message(); 
        tblDKMHDotDKNhom = tblDKMHDotDKNhomService.gettblDKMHDotDKNhom(new Long(id)); 
        tblDKMHDotDKNhom.setMa(ma); 
        tblDKMHDotDKNhom.setTen(ten); 
        tblDKMHDotDKNhom.setDotDKId(tblDKMHDotDKService.gettblDKMHDotDK(new Long(dotDKId))); 
       try{ 
       DateFormat df =  new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
       Date dd = df.parse(ngayBatDau); 
       GregorianCalendar c = new GregorianCalendar(); 
       c.setTime(dd);
       XMLGregorianCalendar d = DatatypeFactory.newInstance().newXMLGregorianCalendar(c); 
        tblDKMHDotDKNhom.setNgayBatDau(d); 
}catch(Exception e){  e.printStackTrace(); } 
        try{ 
       DateFormat df =  new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
       Date dd = df.parse(ngayKetThuc); 
       GregorianCalendar c = new GregorianCalendar(); 
       c.setTime(dd);
       XMLGregorianCalendar d = DatatypeFactory.newInstance().newXMLGregorianCalendar(c); 
        tblDKMHDotDKNhom.setNgayKetThuc(d); 
}catch(Exception e){  e.printStackTrace(); } 
         tblDKMHDotDKNhom.setQuyen(quyen); 
        preUpdate(msg,tblDKMHDotDKNhom); 
       if (msg.isPreMessage_b()) 
       { 
           tblDKMHDotDKNhomService.updatetblDKMHDotDKNhom(tblDKMHDotDKNhom); 
           tblDKMHDotDKNhomService.calSPUpdateChiTietHVDangKyTheoNhomDK(tblDKMHDotDKNhom.getId());
           
           postUpdate(msg, tblDKMHDotDKNhom); 
           if (msg.isPostMessage_b() == false) 
           { 
               return Action.ERROR; 
           } 
       }else{ 
           return Action.ERROR; 
       } 

       List<ConditionReport> cons = new ArrayList<ConditionReport>(); 
       cons.clear(); 
       if (id != 0) 
       { 
           ConditionReport con = new ConditionReport(); 
           con.setCol("nhomDKId.id"); 
           con.setValue1(new Long(id)); 
           con.setOperator("eq"); 
           cons.add(con); 
       } 
       tblDKMHDotDKHocVienChiTiets = tblDKMHDotDKHocVienChiTietService.findtblDKMHDotDKHocVienChiTietByCondition(cons, pageSize, page);
       if (pageSize == 0) 
       { 
           pageSize = 20; 
       } 
       if (page == 0) 
       { 
           page = 1; 
       } 
        pageTblDKMHDotDKHocVienChiTiet = page; 
        pageSizeTblDKMHDotDKHocVienChiTiet = pageSize; 
     
        pagesTblDKMHDotDKHocVienChiTiet =tblDKMHDotDKHocVienChiTietService.counttblDKMHDotDKHocVienChiTietByCondition(cons)/pageSizeTblDKMHDotDKHocVienChiTiet + 1; 
        
       
        return Action.SUCCESS; 
      } 
      catch(Exception e){ 
         System.out.println("Loi Update tbl_DKMH_DotDK_Nhom Action"); 
         e.printStackTrace(); 
         msg.setPostMessage_b(false); 
         msg.setPostMessage_s(e.getMessage()); 
         return Action.ERROR; 
 } 
 } 
    private Collection<TblDKMHDotDKHocVienChiTiet> tblDKMHDotDKHocVienChiTiets; 
    public Collection<TblDKMHDotDKHocVienChiTiet>  getTblDKMHDotDKHocVienChiTiets(){ 
              return tblDKMHDotDKHocVienChiTiets; 
     } 
    private TblDKMHDotDKHocVienChiTietService tblDKMHDotDKHocVienChiTietService; 
    private int pageSizeTblDKMHDotDKHocVienChiTiet; 
    public int getPageSizeTblDKMHDotDKHocVienChiTiet() {
        return pageSizeTblDKMHDotDKHocVienChiTiet;
 }

    public void setPageSizeTblDKMHDotDKHocVienChiTiet(int pageSizeTblDKMHDotDKHocVienChiTiet) {        this.pageSizeTblDKMHDotDKHocVienChiTiet = pageSizeTblDKMHDotDKHocVienChiTiet; }

    private int pageTblDKMHDotDKHocVienChiTiet; 
    public int getPageTblDKMHDotDKHocVienChiTiet() { return pageTblDKMHDotDKHocVienChiTiet; }

    public void setPageTblDKMHDotDKHocVienChiTiet(int pageTblDKMHDotDKHocVienChiTiet) { this.pageTblDKMHDotDKHocVienChiTiet = pageTblDKMHDotDKHocVienChiTiet; }

    private int pagesTblDKMHDotDKHocVienChiTiet; 
    public int getPagesTblDKMHDotDKHocVienChiTiet() { return pagesTblDKMHDotDKHocVienChiTiet; }

    public void setPagesTblDKMHDotDKHocVienChiTiet(int pagesTblDKMHDotDKHocVienChiTiet) { this.pagesTblDKMHDotDKHocVienChiTiet = pagesTblDKMHDotDKHocVienChiTiet; }

 } 
 
